package fr.fermat.lyricsext;

import fr.fermat.control.Controller;
import fr.fermat.databaseManager.LyricManager;
import fr.fermat.databaseManager.Lyrics;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class EditFragment extends Fragment {

	private Controller controller;
	private TextView info;
	private Button save;
	private LyricManager manager;
	private Lyrics lyric;
	private EditText edittext;
	private String lyricView ="" ;
	private boolean flag = false;

	public EditFragment(Controller controller) {
		super();
		this.controller = controller;
	}
	
	public EditFragment() {
		super();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	/**
	 * Initializes the Fragment
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.edit, container, false);
		info = (TextView)view.findViewById(R.id.info);
		info.setText(((MainActivity)getActivity()).getArtistName()+" "
		               +((MainActivity)getActivity()).getTrackName());
		
		initialize(view);
		
		return view;
	}
	
	public TextView getInfo() {
		return info;
	}

	public void setInfo(TextView info) {
		this.info = info;
	}
	

	private void initialize(final View view) {
		edittext = (EditText) view.findViewById(R.id.editText1); 
		save = (Button) view.findViewById(R.id.button1);
		
		save.setOnClickListener(new OnClickListener() {
        
			@Override
			public void onClick(View v) {	
				if (controller.isConnectingToInternet((MainActivity)getActivity())) {
					
					lyric = new Lyrics(((MainActivity)getActivity()).getArtistName(),
        		            ((MainActivity)getActivity()).getTrackName(),
        		            ((MainActivity)getActivity()).getAlbumName(),
        		            edittext.getText().toString());
					lyricView = edittext.getText().toString();
					try {
						 new Thread(new Runnable() {
						     public void run() {
						    	controller.getService().sendLyricsToServer(lyric, (MainActivity)getActivity()) ;
						     } 
						 }).start();
					} catch (Exception e) {
						
					}
				}
				try {
			     	 manager = LyricManager.getInstance((MainActivity)getActivity());
			    	 long verif = manager.putLyric(lyric);
			    	 if (verif == -1) {
			    		 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
			    				 (MainActivity)getActivity());
			    		 alertDialogBuilder
			    		    .setTitle(R.string.titel_dialog)
							.setMessage(R.string.lyric_update_question)
							.setIcon(android.R.drawable.ic_dialog_alert)
							.setCancelable(false)
							.setPositiveButton(R.string.yes_dialog_button,new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int id) {
									// if this button is clicked, close
									// current activity
									if (manager.updateLyric(lyric) == 1) {
										MainActivity.RunToMe("Update done !", getActivity());
									}
								}
							  })
							.setNegativeButton(R.string.no_dialog_button,new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int id) {
									dialog.cancel();
								}
							});
			    		// create alert dialog
							AlertDialog alertDialog = alertDialogBuilder.create();
							alertDialog.show();
						} else {
       						MainActivity.RunToMe("lyric insert in the Database", getActivity());
						}
			    	 flag = true;
				} catch (Exception e) {
					Log.e("Database", "Table error");
				}
				if (flag) {
					controller.setLyricView(lyricView);
					((MainActivity)getActivity()).setLyricView(lyricView);
				}
			}
			
		});
	}
}
